package sk.tuke.homescreen;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.util.Log;

import sk.tuke.homescreen.persistency.InternalDAO;

public class QuickPickAssignments {

	private static String TAG = "QuickPickAssignments";
	private HashMap<Integer, String> qpAssignments = new HashMap<Integer, String>();
	private InternalDAO mStorage = null;
	
	public QuickPickAssignments(Context context){
		mStorage = new InternalDAO(context);
		load();
	}
	
	public void load(){
		List<AssignmentPair> assignments = mStorage.getAssignments();
		for(AssignmentPair p: assignments){
			if(!qpAssignments.containsKey(p.getCount())){
				qpAssignments.put(p.getCount(), p.getAppName());
			}
		}
	}
	
	public void addAssignment(int key, String appName){
		if(this.qpAssignments.containsKey(key)){
			removeAssignment(key);
		}
		this.qpAssignments.put(key, appName);
		try{
			this.mStorage.insertApp(appName, key);
		}catch(Exception ex){
			Log.e(TAG, ex.getMessage());
		}
	}
	
	public void removeAssignment(int key){
		if(this.qpAssignments.containsKey(key)){
			this.qpAssignments.remove(key);
			try{
				mStorage.removeApp(key);
			}catch(Exception ex){
				Log.e(TAG, ex.getMessage());
			}
		}
	}
	
	public String getAssignment(int key){
		if(qpAssignments.containsKey(key)){
			return qpAssignments.get(key);
		}else{
			return "";
		}
	}
}
